package cn.onlineclass.service;

import cn.onlineclass.dao.UserMapper;
import cn.onlineclass.entity.OUser;
import cn.onlineclass.util.MybatisUtil;
import cn.onlineclass.vo.AjaxPage;
import cn.onlineclass.vo.AjaxResult;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class OUserService {


    public static final OUserService TYPE = new OUserService();

    public AjaxPage search(int pageNo, int pageSize, String name, String wechat) {
        SqlSession s = MybatisUtil.get();
        try {
            UserMapper mapper = s.getMapper(UserMapper.class);
            List<OUser> users = mapper.search(pageNo, pageSize, name, wechat);
            int total = mapper.searchTotal(name, wechat);
            return new AjaxPage(users, total, 10, pageNo);
        } finally {
            MybatisUtil.close(s);
        }
    }

    public AjaxResult add(OUser user) {
        SqlSession s = MybatisUtil.get();
        try {
            UserMapper mapper = s.getMapper(UserMapper.class);
            int add = mapper.add(user);
            return AjaxResult.success(add);
        } finally {
            MybatisUtil.close(s);
        }

    }

    public AjaxResult update(OUser user) {
        SqlSession s = MybatisUtil.get(true);
        try {
            UserMapper mapper = s.getMapper(UserMapper.class);
            int update = mapper.update(user);
            return AjaxResult.success(update);
        } finally {

            MybatisUtil.close(s);
        }
    }

    public AjaxResult del(int id) {
        SqlSession s = MybatisUtil.get(true);
        try {
            UserMapper mapper = s.getMapper(UserMapper.class);
            int del = mapper.del(id);
            return AjaxResult.success(del);
        } finally {

            MybatisUtil.close(s);
        }
    }


    public AjaxResult get(long id){
        SqlSession session = MybatisUtil.get();
        try {
            UserMapper mapper = session.getMapper(UserMapper.class);
            return AjaxResult.success(mapper.get(id));
        }finally {
            MybatisUtil.close(session);
        }
    }


}
